package com.igbeok.miscellaneous.concurrency.thij;

public class WaitPerson2 extends Thread {
	private Restaurant restaurant;
	private boolean isWaiting = false;

	public WaitPerson2(String name, Restaurant r) {
		super(name);
		this.restaurant = r;
	}

	public void run() {
		while (true) {
			while (restaurant.orders.isEmpty()) {
				synchronized (restaurant.orders) {
					try {
						System.out.println(this + ": Order没有了, 正在等待Order....");
						isWaiting = true;
						wait();
					} catch (InterruptedException e) {
						throw new RuntimeException(e);
					}

					Order2 order = restaurant.orders.get(0);
					System.out.println(this + ": Waitperson得到了 " + order);
					isWaiting = false;
					restaurant.orders.remove(order);
				}
			}
		}
	}

	public boolean isWaiting() {
		return isWaiting;
	}
}
